//
//  Alert.swift
//  Daily
//
//  Created by xhl_dabaoMac on 2021/11/5.
//

import UIKit
import SnapKit

public  class Alert {

    static let _loadingView = loadingView()
    static let _window = UIApplication.shared.delegate?.window!
    static let _circleView = CircleProgressView(frame: CGRect(x: 0, y: 0, width: 80, height: 80))
    
    ///文本消息提示
    public static func show(_ message:String,
                     _ delay : TimeInterval = 2){
        
        let mv = MsaageView()
        mv.titleLabel.text = message;
        _window?.addSubview(mv)
        mv.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.lessThanOrEqualTo(_window?.bounds.size.width ?? 0 - 20)
        }
        DispatchQueue.global().asyncAfter(deadline: .now()+delay) {
            DispatchQueue.main.async {
                mv.removeFromSuperview()
            }
        }
    }
    
    ///阻挡交互的菊花。开始转圈
    public static func loading(){
        _window?.addSubview(_loadingView)
        _loadingView.startAnimating()
    }
    //隐藏菊花
    public static func hide(){
        _loadingView.stopAnimating()
        _loadingView.removeFromSuperview()
    }
    
    ///圆形进度条。屏幕中心
    public static func progress( _ progress : CGFloat){
        
        if _circleView.superview == nil {
            _window?.addSubview(_circleView)
            _window?.bringSubviewToFront(_circleView)
            _circleView.snp.makeConstraints { make in
                make.center.equalToSuperview()
            }
        }
        _circleView.progress = progress
        
        if progress >= 1 {
            _circleView.removeFromSuperview()
        }
    }
    
}
///菊花view
class loadingView: UIView {
    
    private let indicatorView : UIActivityIndicatorView = {
        let indicatorView = UIActivityIndicatorView(frame: CGRect.zero)
        indicatorView.style = .gray
        indicatorView.transform = CGAffineTransform(scaleX: 3, y: 3)
        return indicatorView
    }()
    private override init(frame: CGRect) {
        super .init(frame: UIScreen.main.bounds)
        
        addSubview(indicatorView)
        indicatorView.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
    }
    
    func startAnimating(){ indicatorView.startAnimating() }
    func stopAnimating(){ indicatorView.stopAnimating() }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

///文本信息view
class MsaageView: UIView {
    var titleLabel : UILabel = {
        let label = UILabel()
        label.font =  UIFont.systemFont(ofSize: 20)
        label.textColor = UIColor.black
        label.numberOfLines = 0
        return label
    }()
    
    override init(frame: CGRect) {
        super .init(frame: frame)
        self.layer.cornerRadius = 5
        self.layer.masksToBounds = true
        self.backgroundColor = UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 0.5)
        addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.top.left.equalToSuperview().offset(20);
            make.bottom.right.equalToSuperview().offset(-20);
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

///圆形进度条view
class CircleProgressView: UIView {
    
    var progress: CGFloat = 0 {
        didSet {
            if progress <= 1,progress >= 0  {
                DispatchQueue.main.async {
                    self.setNeedsDisplay()
                }
            }
        }
    }
    var lineWidth: CGFloat = 5
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .clear

    }
    override func draw(_ rect: CGRect) {
        
        let bottomPath = UIBezierPath(arcCenter: CGPoint(x: bounds.width * 0.5, y: bounds.height * 0.5), radius: (bounds.width - lineWidth) * 0.5, startAngle: 0, endAngle: .pi * 2, clockwise: true)
        bottomPath.lineWidth = lineWidth
        UIColor.init(white: 0, alpha: 0.2).setStroke()
        UIColor.clear.setFill()
        bottomPath.stroke()
        bottomPath.fill()
        
        let path = UIBezierPath(arcCenter: CGPoint(x: bounds.width * 0.5, y: bounds.height * 0.5), radius: (bounds.width - lineWidth) * 0.5, startAngle: .pi * 3 / 2, endAngle: .pi * 2 * progress - .pi / 2, clockwise: true)
        path.lineWidth = lineWidth
        UIColor.red.setStroke()
        path.stroke()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
